import { reqGetAllChannels, reqGetChannels } from '@/api/channels'
const state = {
  channels: [],
  allChannels: []
}
const mutations = {
  setChannels (state, channels) {
    state.channels = channels
  },
  setAllChannels (state, allChannels) {
    state.allChannels = allChannels
  },
  addChannel (state, channel) {
    state.channels.push(channel)
  },
  delChannel (state, channel) {
    state.channels = state.channels.filter(item =>
      item.id !== channel.id
    )
  }
}
const actions = {
  async getMyChannelsAsync (context) {
    const res = await reqGetChannels()
    console.log(res)
    context.commit('setChannels', res.data.data.channels)
  },
  async getAllChannelsAsync (context) {
    const res = await reqGetAllChannels()
    context.commit('setAllChannels', res.data.data.channels)
    console.log(res.data.data.channels)
  }
}
const getters = {
  optionalChannels (state) {
    console.log(1111)
    return state.allChannels.filter(item => state.channels.findIndex(channel => channel.id === item.id) === -1)
  }
}
export default {
  state,
  mutations,
  actions,
  getters,
  namespaced: true
}
